package org.theonept.travian.model;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.theonept.travian.util.HttpUtil;

public class AbstractBatiment extends Entity {

	private String id;
	private String level;
	private Integer costBois;
	private Integer costArgile;
	private Integer costFer;
	protected Integer costCereal;
	private Integer consoCereal;
	private Village village;

	public AbstractBatiment() {
		super();
	}

	/**
	 * @return the costBois
	 */
	public Integer getCostBois() {
		return costBois;
	}

	/**
	 * @param costBois
	 *            the costBois to set
	 */
	public void setCostBois(Integer costBois) {
		this.costBois = costBois;
	}

	/**
	 * @return the costArgile
	 */
	public Integer getCostArgile() {
		return costArgile;
	}

	/**
	 * @param costArgile
	 *            the costArgile to set
	 */
	public void setCostArgile(Integer costArgile) {
		this.costArgile = costArgile;
	}

	/**
	 * @return the costFer
	 */
	public Integer getCostFer() {
		return costFer;
	}

	/**
	 * @param costFer
	 *            the costFer to set
	 */
	public void setCostFer(Integer costFer) {
		this.costFer = costFer;
	}

	/**
	 * @param costCereal
	 *            the costCereal to set
	 */
	public void setCostCereal(Integer costCereal) {
		this.costCereal = costCereal;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	/**
	 * @return the consoCereal
	 */
	public Integer getConsoCereal() {
		return consoCereal;
	}

	/**
	 * @param consoCereal
	 *            the consoCereal to set
	 */
	public void setConsoCereal(Integer consoCereal) {
		this.consoCereal = consoCereal;
	}

	public boolean up() {

		getVillage().getProduction(true);
		if (isUpable()) {
			HttpUtil.getDocumentFromUrl(getVillage().getCompte()
					.getUrlServeur()
					+ "dorf1.php?newdid="
					+ getVillage().getIdVillage());
			Document documentRessourcePrecise = HttpUtil
					.getDocumentFromUrl(getVillage().getCompte()
							.getUrlServeur() + "build.php?id=" + id);

			Element elementForm = documentRessourcePrecise
					.getElementById("contract")
					.getElementsByAttributeValue("class", "contractLink")
					.first()
					.getElementsByAttributeValue("type", "text/javascript")
					.first();

			String urlToUp = getVillage().getCompte().getUrlServeur()
					+ "dorf1.php?a=" + id + "&c="
					+ elementForm.html().split(";c=")[1].substring(0, 6);
			HttpUtil.getDocumentFromUrl(urlToUp);
			getVillage().getProduction(true).getRessources();
			return true;
		} else {
			return false;
		}

	}

	public boolean isUpable() {
		if (getVillage().getConstructionEnCours()
				|| (costArgile == null && costFer == null && costCereal == null && costBois == null)) {
			return false;
		}
		return costArgile < getVillage().getProduction().getStockArgile()
				&& costFer < getVillage().getProduction().getStockFer()
				&& costBois < getVillage().getProduction().getStockBois()
				&& costCereal < getVillage().getProduction().getStockCereale();
	}

	/**
	 * @return the village
	 */
	public Village getVillage() {
		return village;
	}

	/**
	 * @param village
	 *            the village to set
	 */
	public void setVillage(Village village) {
		this.village = village;
	}

}